<?php
/*
菜单管理 或 模块管理
*/
class MenuController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	
	public function init() {
		
		$this->commonAction();	
	
	}
	//用户首页
	public function indexAction() {
		try {
			$menulist_tmp=$this->_dbAdapter->fetchAssoc('select * from menu order by orderby desc');
			$menulist=array();
			if($menulist_tmp)
			foreach($menulist_tmp as $key => $value){
				$menulist[$value['fathermenu']][]=$value;
			}
			unset($menulist_tmp);
			//print_r($menulist);exit;
			$this->view->menulist=$menulist;
			
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
//添加	
	public function opaddAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			$p =$this->_request->getPost ();
			//print_r($p);exit;
			//Array ( [account] => 0 [name] => ggg [herf] => ggg [istopbar] => on [ishidden] => on )   
			if(isset($p['istopbar'])){
				$p['istopbar']=1;	
			}else{
				$p['istopbar']=0;	
			}
			
			if(isset($p['ishidden'])){
				$p['ishidden']=0;	
			}else{
				$p['ishidden']=1;	
			}
			
			$data=array(
				'menu'=>$p['name'],
				'menu_en'=>$p['href'],
				'istopbar'=>$p['istopbar'],
				'ishidden'=>$p['ishidden'],
				'fathermenu'=>$p['account'],
				'addtime'=>time(),
			);
			$result=$this->_dbAdapter->insert('menu',$data);
			if($result)
				$this->feedback ( '添加成功', '注意', '/'.$this->controller.'/index', 'warning' );
			else	
				throw new Exception ( '添加失败！' );
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
//删除
	public function deleteAction(){
		try {
			$id = $this->_request->getParam ( 'id' );
			if(!$id)
				throw new Exception ( '参数错误' );
			
			if($id){
				$sql="select * from menu where menuid='".$id."'";
				$res=$this->_dbAdapter->fetchRow($sql);

				if(!$res['fathermenu']){
					$sql="select * from menu where fathermenu='".$res['menuid']."'";
					$result=$this->_dbAdapter->fetchAll($sql);
					if($result){
						throw new Exception ( '有子目录不能删除！' );//操作失败,或已经删除，请先刷新查看！
					}
				}
				
				$res=$this->_dbAdapter->delete('menu'," menuid='".$id."'");
			}	
				
			if ($res) {
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/index', 'tip', 3 );
			} else {
				throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
			}
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

//编辑
	public function editAction(){
		try {
			$id = $this->_request->getParam ( 'id' );
			if(!$id)
				throw new Exception ( '参数错误' );
			//print_r($id);exit;
			if($id){
				$sql="select * from menu where menuid='".$id."'";
				$res=$this->_dbAdapter->fetchRow($sql);
				$this->view->res=$res;
				$sql="select * from menu where fathermenu=0";
				$fathermenu=$this->_dbAdapter->fetchAll($sql);
				$this->view->fathermenu=$fathermenu;
				//print_r($fathermenu);exit;
			}	
				
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

//保存编辑
	public function opeditAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			$p =$this->_request->getPost ();

			
			if(isset($p['istopbar'])){
				$p['istopbar']=1;	
			}else{
				$p['istopbar']=0;	
			}
			
			if(isset($p['ishidden'])){
				$p['ishidden']=0;	
			}else{
				$p['ishidden']=1;	
			}

			$data=array(
				'menu'=>$p['name'],
				'menu_en'=>$p['href'],
				'istopbar'=>$p['istopbar'],
				'ishidden'=>$p['ishidden'],
				'fathermenu'=>$p['fid'],
			);
			$res=$this->_dbAdapter->update('menu',$data," menuid='".$p['menuid']."'");
				
			if($res)
				$this->feedback ( '保存成功', '注意', '/'.$this->controller.'/index', 'warning' );
			else	
				throw new Exception ( '更新失败或没有更改任何内容 ！' );
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
//保存修改
	public function opAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			$p =$this->_request->getPost ();
			//print_r($p);exit;
			$isok=0;
			foreach($p['menuid'] as $key=>$value){
				if(isset($p['top'][$value])){
					$istopbar=1;
				}else{
					$istopbar=0;
				}
				if(isset($p['hide'][$value])){
					$ishidden=0;
				}else{
					$ishidden=1;
				}
				$data=array(
					'menu'=>$p['modename'][$key],
					'orderby'=>$p['orderby'][$key],
					'istopbar'=>$istopbar,
					'ishidden'=>$ishidden,
				);
				$res=$this->_dbAdapter->update('menu',$data," menuid='".$value."'");
				if($res){
					$isok=1;
				}
			}
			
			if($isok)
				$this->feedback ( '保存成功', '注意', '/'.$this->controller, 'warning' );
			else	
				throw new Exception ( '更新失败或没有更改任何内容 ！' );
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}


	
	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}}

